package com.why.simpleboot.core.server.netty.resolver.impl;

import com.why.simpleboot.core.annotation.mvc.RequestParam;
import com.why.simpleboot.core.common.utils.GeneralUtlis;
import com.why.simpleboot.core.core.entity.MethodDefinition;
import com.why.simpleboot.core.server.netty.resolver.ParameterResolver;

import java.lang.reflect.Parameter;
import java.util.Map;

/**
 * @program: why-simpleboot-framework
 * @description:
 * @author: @why
 * @create: 2021-05-23 17:30
 **/
public class RequestParamParameterResolver implements ParameterResolver {

    /**
     * @param methodDefinition
     * @param parameter
     * @return
     * @see ParameterResolver
     */
    @Override
    public Object resolve(MethodDefinition methodDefinition, Parameter parameter) {
        Map<String, String> parameterMappings = methodDefinition.getRequestParameterMappings();
        RequestParam requestParam = parameter.getAnnotation(RequestParam.class);

        String name = requestParam.value();
        name = "".equals(name) ? parameter.getName() : requestParam.value();

        String result = parameterMappings.get(name);
        if(result==null){
            throw new IllegalArgumentException("ReqeustParam解析参数失败");
        }
        return GeneralUtlis.convert(parameter.getType(), result);
    }


}
